package cn.ffcs.share.service;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

import cn.ffcs.security.model.User;
import cn.ffcs.share.dao.ShareReferenceDAO;
import cn.ffcs.share.model.ShareReference;
import cn.ffcs.util.context.ApplicationContextUtil;

import cn.ffcs.base.service.BaseService;
import cn.ffcs.common.dao.AttachDAO;
import cn.ffcs.common.model.Attach;

/**
 * 共享资料服务层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class ShareReferenceService<D extends ShareReference> extends BaseService<D>
implements IShareReferenceService<D>
{
    private ShareReferenceDAO shareReferenceDAO;
    private AttachDAO attachDAO;
    /**
     * 删除操作
     * @param baseEntity
     */
    public void remove(D shareReference){
        if (shareReference!=null){
            Criterion cond1 = Restrictions.eq("refEntity", shareReference.getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityID", shareReference.getId().toString());
            Criterion condAttach = Restrictions.and(cond1, cond2);
            List<Attach> attachs=attachDAO.findBy(condAttach);
            for (Attach att : attachs)
            {
                attachDAO.deleteById(att.getId()+"");
            }
            super.remove(shareReference);
        }
    }
    /**
     * 批量删除操作
     * @param baseEntity
     */
    public void batchDel(String[] ids){
        for (String id : ids){
            D shareReference=this.findById(new Integer(id));
            remove(shareReference);
        }
    }

    /**
     * 新增或修改操作
     * @param baseEntity
     */
    public void saveOrUpdate(D shareReference)
    {
        if (shareReference != null)
        {
            if (shareReference.getId() == null || shareReference.getId() == 0)
            {   
                if("".equals(shareReference.getCreator().trim())){
                    //设置操作人
                    User currUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
                    if (currUser!=null)
                        shareReference.setCreator(currUser.getUserID());
                }
                //设置有效时间
                shareReference.setBegin(shareReferenceDAO.getCurrSysdate());
                Long l=Long.valueOf("9783941568000");
                Date end=new Date(l);
                shareReference.setEnd(end);
                //保存数据
                shareReferenceDAO.save(shareReference);
            }
            else
            {
                shareReferenceDAO.update(shareReference);
            }
        }
    }
    
    public List findShareReferenceByType(String dictType,String count){
        return shareReferenceDAO.findShareReferenceByType(dictType, count);
    }
    
    public AttachDAO getAttachDAO()
    {
        return attachDAO;
    }
    public void setAttachDAO(AttachDAO attachDAO)
    {
        this.attachDAO = attachDAO;
    }
    public ShareReferenceDAO getShareReferenceDAO()
    {
        return shareReferenceDAO;
    }
    public void setShareReferenceDAO(ShareReferenceDAO shareReferenceDAO)
    {
        this.shareReferenceDAO = shareReferenceDAO;
    }
    
}
